'use strict';

const BaseController = require('./base');

class UserAndRoleController extends BaseController {
  get _service() {
    return 'userAndRole';
  }

  async update() {
    const { ctx } = this;
    const { rid } = ctx.params();
    const { id } = ctx.params;
    const result = await this.service.userAndRole.update({ rid, id });
    if (result) {
      this.success();
    } else {
      this.error('更新权限失败');
    }
  }

  async show() {
    await this._show('获取权限失败');
  }

  async index() {
    const { ctx } = this;
    const { rid, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const tempResult = await this.service.userAndRole.findList({ rid, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取权限失败');
    }
  }
}

module.exports = UserAndRoleController;
